<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pemesanan extends CI_Controller {
	private $limit=10;

	function __construct(){
		parent::__construct();
		
		$index	= $this->config->item('index_page');
		$host	= $this->config->item('base_url');

		$this->url = empty($index) ? $host : $host . $index . '/';
		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
		$this->load->model('mPemesanan');
		
		//get session.. 'user' tuh nama session yang udah di set 
//		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
	}
	
	public function index($offset=0,$order_column='no_order', $order_type='asc'){
	if (!empty($this->user)){
		if(empty($offset)) $offset=0;
		if(empty($order_column)) $order_column='no_order';
		if(empty($order_type)) $order_type='asc';
		//todo : check for valid_barang column

		//load data siswa
		$pemesanan=$this->mPemesanan->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
		//generate pagination
		$config['base_url']=site_url('admin/pemesanan/index/');
		$config['total_rows']=$this->mPemesanan->count_all();
		$config['per_page']=$this->limit;
		$config['uri_segment']=3;
		$this->pagination->initialize($config);
		$data['pagination']=$this->pagination->create_links();

		//generate table data
		$this->table->set_empty("&nbsp;");
		$new_order=($order_type=='asc'?'desc':'asc');
		$this->table->set_heading(
			'NO ORDER','USERNAME','ADMINNAME','TANGGAL ORDER','STATUS BAYAR', 'TANGGAL DIPROSES','TOTAL HARGA','AKSI');
		foreach($pemesanan as $pesan){
				$this->table->add_row($pesan->NO_ORDER,$pesan->USERNAME,$pesan->ADMINNAME,$pesan->TGL_ORDER,$pesan->STATUS_BAYAR,$pesan->TGL_DIPROSES,$pesan->TOTAL_HARGA,anchor('admin/pemesanan/update/'.$pesan->NO_ORDER,'update',array('class'=>'update')).' '.anchor('admin/pemesanan/delete/'.$pesan->NO_ORDER,'delete',array('class'=>'delete','onclick'=>"return confirm('Apakah Anda yakin ingin menghapus data kategori?')"))
				);
		}
			
			$data['table']=$this->table->generate();
			
			if($this->uri->segment(3)=='delete success') $data['message']='Data berhasil dihapus';
			else if ($this->uri->segment(3)=='add_success') $data['message']='Data berhasil ditambah';
			else $data['message']='';
			//load view

			$data['url'] = $this->url;
			$data['header'] = $this->load->view('admin/header/header', $data, true);
			$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
			$data['content'] = $this->load->view('admin/rcontent/pemesanan/pemesanan', $data, true);
			$this->load->view('index', $data);
		}
	   else
	   {
		 //If no session, redirect to login page
		 redirect('admin/login');
	   }
	}
	
	function update($no_order){
	//set common properties
		$data['title']='Update Pemesanan';
	
	//set validation properties
		$this->_set_rules();
		$data['action']=('admin/pemesanan/update/'.$no_order);
		
		//run validation
		if($this->form_validation->run()===FALSE){
			$data['message']='';
			$data['pemesanan']=$this->mPemesanan->get_by_id($no_order)->row_array();
			
			//set common properties
			$data['title']='Update Pemesanan';
			$data['message']='';
		}else {
			//save data
			$no_order=$this->input->post('no_order');
			$pemesanan=array('status_bayar'=>$this->input->post('status'),'tgl_diproses'=>$this->input->post('tgl_diproses'));
			$this->mPemesanan->update($no_order,$pemesanan);
			$data['pemesanan']=$this->mPemesanan->get_by_id($no_order)->row_array();
			
			//set user message
			$data['message']='update pemesanan berhasil';
		}
		$data['link_back']=anchor('admin/pemesanan/index/','Lihat daftar pemesanan',array('class'=>'back'));
		//load view
		$data['url'] = $this->url;
		$data['header'] = $this->load->view('admin/header/header', $data, true);
		$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
		$data['content'] = $this->load->view('admin/rcontent/pemesanan/pemesananEdit', $data, true);

		$this->load->view('index',$data);
	}
	
	function delete($no_order){
		//delete siswa
		$this->mPemesanan->delete($no_order);
		//redirect to siswa list page
		redirect('admin/pemesanan/index/delete_success','refresh');
	}
	
	//valid_barangation rules
	function _set_rules(){
		$this->form_validation->set_rules('tgl_diproses','tgl_diproses','required|trim');
	}
	
}